package com.team2.outpatient_sys.web;

import com.github.pagehelper.PageInfo;
import com.team2.outpatient_sys.dao.DepartmentDao;
import com.team2.outpatient_sys.dao.DoctorDao;
import com.team2.outpatient_sys.dao.PatientDao;
import com.team2.outpatient_sys.pojo.*;
import com.team2.outpatient_sys.service.AdminDepartmentService;
import com.team2.outpatient_sys.service.OutpatientScheduleService;
import com.team2.outpatient_sys.service.PatientService;
import com.team2.outpatient_sys.service.RegisteredService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@RestController
//@CrossOrigin
@RequestMapping("/registereds")
public class RegisteredController {
    @Autowired
    private RegisteredService registeredService;
    @Autowired
    private PatientDao patientDao;
    @Autowired
    private DepartmentDao departmentDao;
    @Autowired
    private DoctorDao doctorDao;
    //线上挂号
    @PutMapping("/add")
    public Result add(@RequestBody Registered registered) {
        Result result = new Result();
        if (registeredService.add(registered)) {
            result.setCode(200);;
            result.setMsg("添加成功");
            result.setData(registered);
        } else {
            result.setCode(500);;
            result.setMsg("添加失败");
        }
        return result;
    }

    //更改挂号状态
    @PostMapping("/update")
    public Result update(@RequestBody Registered registered) {
        Result result = new Result();
        if (registeredService.update(registered)) {
            result.setCode(200);
            result.setMsg("修改成功");
        } else {
            result.setCode(500);
            result.setMsg("修改失败");
        }
        return result;
    }

    //查看所有挂号信息
    @GetMapping("/findAll")
    public Result findAll(){
        Result result = new Result();
        List<Registered> list = registeredService.findAll();
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }
    //删除挂号信息
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") int id) {
        Result result = new Result();
        if (!registeredService.delete(id)) {
            result.setCode(500);
            result.setMsg("删除失败");
        } else {
            result.setCode(200);
            result.setMsg("删除成功");
        }
        return result;
    }
    //根据id查询挂号信息
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable("id") int id) {
        Result result = new Result();
        Registered registered = registeredService.findById(id);
        if (registered == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(registered);
        }
        return result;
    }

    //根据病人id查询挂号信息
    @GetMapping("/findByPatientId/{patientId}")
    public Result findByPatientId(@PathVariable("patientId") int patientId) {
        Result result = new Result();
        List<Registered> list = registeredService.findByPatientId(patientId);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }

    //根据医生查询所有挂号信息
    @GetMapping("/findByDoctorId/{doctorId}")
    public Result findByDoctorId(@PathVariable("doctorId") int doctorId) {
        Result result = new Result();
        List<Registered> list = registeredService.findByDoctorId(doctorId);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }

    //根据科室查看所有挂号信息
    @GetMapping("/findByDeptId/{deptId}")
    public Result findByDeptId(@PathVariable("deptId") int deptId) {
        Result result = new Result();
        List<Registered> list = registeredService.findByDeptId(deptId);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }

    //根据就诊时间查看所有挂号信息
    @GetMapping("/findByClinicTime/{clinicTime}")
    public Result findByDeptId(@PathVariable("clinicTime") String clinicTime) {
        Result result = new Result();
        List<Registered> list = registeredService.findByClinicTime(clinicTime);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }

    //线下挂号
    @PutMapping("/addOffline")
    public Result addOffline(@RequestBody RegisteredOffline registeredOffline) {
        Result result = new Result();
        if (registeredService.offlineAdd(registeredOffline)) {
            result.setCode(200);;
            result.setMsg("添加成功");
            result.setData(registeredOffline);
        } else {
            result.setCode(500);;
            result.setMsg("添加失败");
        }
        return result;
    }

    //查看待签到挂号信息
    @GetMapping("/signIn/{currentPage}/{pageSize}")
    public Result signIn(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        Result result = new Result();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
        String clinicTime = sdf.format(date);
        List<Registered> list = registeredService.findSign(clinicTime,currentPage,pageSize);
        PageInfo<Registered> pageInfo = new PageInfo<>(list);
        List<RegisteredDeptAndDoctor> lists = new ArrayList<>();
        for (Registered registered : list) {
            int id = registered.getId();
            int patientId = registered.getPatientId();
            String patient = patientDao.findById(patientId).getName();
            String idCard = patientDao.findById(patientId).getIdCard();
            int deptId = registered.getDeptId();
            String dept = departmentDao.queryById(deptId).getName();
            int doctorId = registered.getDoctorId();
            String doctor = doctorDao.queryById(doctorId).getName();
            RegisteredDeptAndDoctor registeredDeptAndDoctor = new RegisteredDeptAndDoctor(id, patientId, patient, idCard, deptId, dept, doctorId, doctor, clinicTime);
            lists.add(registeredDeptAndDoctor);
        }
        PageInfo<RegisteredDeptAndDoctor> pageInfo1 = new PageInfo<>(lists);
        BeanUtils.copyProperties(pageInfo,pageInfo1);
        pageInfo1.setList(lists);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo1);
        }
        return result;
    }

    //查看所有挂号详情
    @GetMapping("/findRegisteredDetailed/{currentPage}/{pageSize}")
    public Result findRegisteredDetailed(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        Result result = new Result();
        List<RegisteredDetailed> list = registeredService.findRegisteredDetailed(currentPage, pageSize);

        PageInfo<RegisteredDetailed> pageInfo = new PageInfo<>(list);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(pageInfo);
        }
        return result;
    }
    //检查过期的挂号
    @GetMapping("/overdue")
    public void overdue(){
        Result result = new Result();
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d");
        String clinicTime = sdf.format(date);
        registeredService.overdue(clinicTime);
    }

    //查询当天排班医生
    @GetMapping("/findDoctor/{week}/{deptId}")
    public Result findDoctor(@PathVariable Integer week,@PathVariable Integer deptId){
        Result result = new Result();
        List<Doctor> list = registeredService.findDoctor(week, deptId);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }
    //查询当天排班医生
    @GetMapping("/findDoctorShift/{week}/{deptId}/{doctorId}")
    public Result findDoctor(@PathVariable Integer week,@PathVariable Integer deptId,@PathVariable Integer doctorId){
        Result result = new Result();
        List<DoctorShift> list = registeredService.findDoctorShift(week, deptId,doctorId);
        if (list == null) {
            result.setCode(500);
            result.setMsg("查询失败");
        } else {
            result.setCode(200);
            result.setMsg("查询成功");
            result.setData(list);
        }
        return result;
    }
    //查询当天排班医生
    @PostMapping("/registeredSuccess/{doctorId}/{scheduleId}")
    public Result registeredSuccess(@PathVariable Integer doctorId,@PathVariable Integer scheduleId){
        Result result = new Result();
        if (!registeredService.registeredSuccess(doctorId, scheduleId)) {
            result.setCode(500);
            result.setMsg("挂号失败");
        } else {
            result.setCode(200);
            result.setMsg("挂号成功");
        }
        return result;
    }
}
